package com.shop.service.shop.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.service.module.entity.GoodsEntity;
import com.shop.service.module.entity.GoodsTypeEntity;
import com.shop.service.module.entity.Result;
import com.shop.service.module.service.GoodsService;
import com.shop.service.module.service.GoodsTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods-type")
@Slf4j
public class GoodsTypeController {
    @Autowired
    private GoodsTypeService goodsTypeService;

    @GetMapping("list/page")
    public Result getListPage(@RequestParam(value = "pno", defaultValue = "1") Integer pno,
                              @RequestParam(value = "psize", defaultValue = "10") Integer psize,
                              @RequestParam(required = false, value = "name") String name) {
        QueryWrapper<GoodsTypeEntity> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.eq("name", name);
        }
        Page page = new Page(pno, psize);
        Page result = goodsTypeService.page(page, queryWrapper);
        JSONObject data = new JSONObject();
        JSONObject jsonResult = new JSONObject();
        jsonResult.put("pno", pno);
        jsonResult.put("psize", psize);

        jsonResult.put("pCount", result.getPages());
        jsonResult.put("totalElements", result.getTotal());
        data.put("list", result.getRecords());
        data.put("page", jsonResult);
        return Result.end(200, data, "查询成功");
    }

    @PutMapping("insert")
    public Result insert(@RequestBody GoodsTypeEntity goodsEntity) {
        boolean result = goodsTypeService.save(goodsEntity);
        return result ? Result.end(200, "", "添加成功") : Result.end(500, "", "添加失败");
    }

    @GetMapping("find/id/{id}")
    public Result findById(@PathVariable("id") Long id) {
        GoodsTypeEntity goods = goodsTypeService.getById(id);
        return Result.end(200, goods, "查询成功");
    }

    @PutMapping("update")
    public Result update(@RequestBody GoodsTypeEntity goodsEntity) {
        boolean result = goodsTypeService.updateById(goodsEntity);
        return result ? Result.end(200, "", "修改成功") : Result.end(500, "", "修改失败");
    }

    @DeleteMapping("delete/id/{id}")
    public Result delete(@PathVariable("id") Long id) {
        boolean result = goodsTypeService.removeById(id);
        return result ? Result.end(200, "", "删除成功") : Result.end(500, "", "删除失败");

    }

    @GetMapping("/list/all")
    public Result getListAll() {
        List<GoodsTypeEntity> list = goodsTypeService.list();
        JSONObject data = new JSONObject();
        data.put("list",list);
        return Result.end(200, data, "查询成功");

    }

}
